<?php

namespace App\Transformers;

use App\Models\Product;
use League\Fractal\TransformerAbstract;

class ProductTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['company', 'types'];

    public function transform(Product $product)
    {
        return [
            'id' => $product->id,
            'name' => $product->name,
            'company_id' => (int) $product->company_id,
            'thumbnail' => $product->thumbnail,
            'rating' => $product->rating,
            'review_count' => (int) $product->review_count,
            'description' => $product->description,
            'price' => $product->price,
            'created_at' => $product->created_at->toDateTimeString(),
            'updated_at' => $product->updated_at->toDateTimeString(),
        ];
    }

    public function includeCompany(Product $product)
    {
        return $this->item($product->company, new CompanyTransformer());
    }

    public function includeTypes(Product $product)
    {
        $types = $product->types;

        return $this->collection($types, new TypeTransformer());
    }
}